-- tests scene
local layer = nil


--------------------- 类
AchieveClass = {ach=0}
--这句是重定义元表的索引，必须要有，
AchieveClass.__index = AchieveClass
--模拟构造体，一般名称为new()
function AchieveClass:new(_ach)
    local self = {}   
    setmetatable(self, AchieveClass)   --必须要有
    self.ach = _ach
    self.achInfo = Achieve_Infos[_ach]
    self.spr_bg = nil
    self.spr_icon = nil
    
    self.iico = self.achInfo[PStage]
    self.lb_txt = CCLabelTTF:create(self.achInfo[PName], "Arial", FontSize)
    self.lb_txt:setColor(ccc3(100,100,100))--颜色
    self.lb_txt:setAnchorPoint(ccp(0, 0.5))
    
    
    --判定解锁
    self.onUnLock = _AchieveTable_Unlock[_ach]
    
    
    --- 如果成就解锁,则显示白色
    if self.onUnLock then
        self.lb_txt:setColor(ccc3(255, 255, 255))
    end
    
    return self  
end
function AchieveClass:getPosition()

end
function AchieveClass:setPosition(x,y)
    self.spr_bg:setPosition(gdccp(x,y))
    if self.spr_icon ~= nil then
        self.spr_icon:setPosition(gdccp(x-200,y))
    end
    self.lb_txt:setPosition(gdccp(x-100,y))
end
function AchieveClass:addToChild(layer)
    layer:addChild(self.lb_txt)
end




local function initWithLayer(stage)
    -- 按钮回调函数
    local function menuCallback(tag)
    end
    
    -------- ui
    -- 背景
    local _bgSpr = nil
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(_bgSpr)
    
    local layerMain = CCLayer:create()
    
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Achive_bg2)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 960-314))
    layerMain:addChild(_bgSpr)
    
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Achive_title)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 860))
    layerMain:addChild(_bgSpr)
    
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_sbg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 630))
    layerMain:addChild(_bgSpr)
    
    ----数据
    local i = 0
    local invh = 60
    local invw = 95
    local bx, by = 245, DWinSizeHeight - 250
    local lb_sjsk = CCLabelTTF:create("水晶时空最高分", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by))
    layerMain:addChild(lb_sjsk,10)
    
    lb_sjsk = CCLabelTTF:create(GetSJSKDatas(), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by))
    layerMain:addChild(lb_sjsk,10)
    
    --local _lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetSJSKDatas())
    --_lblSoul:setPosition(gdccp(bx + 125, by))
    i = i + 1
    ------
    lb_sjsk = CCLabelTTF:create("拯救国王最高分", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    lb_sjsk = CCLabelTTF:create(GetMaxZJWGScore(), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    --_lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetMaxZJWGScore())
    --_lblSoul:setPosition(gdccp(bx + 125, by - invh * i))
    i = i + 1
    
    ------
    lb_sjsk = CCLabelTTF:create("无限回廊最高分", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    lb_sjsk = CCLabelTTF:create(GetMaxWXHLScore(), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    --_lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetMaxWXHLScore())
    --_lblSoul:setPosition(gdccp(bx + 125, by - invh * i))
    i = i + 1
    
    ----
    lb_sjsk = CCLabelTTF:create("游戏总时间(分)", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    _lblSoul = GameNumbers:new(vNumberTime, NumberAlginment_Left)
    _lblSoul:addLayer(layerMain)
    _lblSoul:setNumber(math.ceil(GetAchieves(DS_GameTime)/60))
    _lblSoul:setPosition(gdccp(bx + 20, by - invh * i))
    
    
    -------sss-----
    bx, by = 470,by
    --invh = 50
    invw = 100
    i = 0
    lb_sjsk = CCLabelTTF:create("击杀敌人数", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    
    lb_sjsk = CCLabelTTF:create(GetAchieves(DS_Skill), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    --_lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetAchieves(DS_Skill))
    --_lblSoul:setPosition(gdccp(bx + invw, by - invh * i))
    i = i + 1
    
    ----
    lb_sjsk = CCLabelTTF:create("水晶币总数", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    lb_sjsk = CCLabelTTF:create(GetCrystal(), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    _lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetCrystal())
    --_lblSoul:setPosition(gdccp(bx + invw, by - invh * i))
    i = i + 1
    
    ----
    lb_sjsk = CCLabelTTF:create("魂力值总数", "Arial", FontSize)
    lb_sjsk:setColor(ccc3(170, 150, 55))--颜色
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    lb_sjsk = CCLabelTTF:create(GetSoulPower(), "Arial", FontSize4)
    lb_sjsk:setAnchorPoint(ccp(1, 0.5))
    lb_sjsk:setPosition(gdccp(bx + invw, by - invh * i))
    layerMain:addChild(lb_sjsk,10)
    
    --_lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Right)
    --_lblSoul:addLayer(layerMain)
    --_lblSoul:setNumber(GetSoulPower())
    --_lblSoul:setPosition(gdccp(bx + invw, by - invh * i))
    i = i + 1
    
    ---已完成与未完成成就总数
    local achUnlock = 0
    local achlock = 0
    table.foreach(_AchieveTable_Unlock, function(v, b)
        if b then
            achUnlock = achUnlock + 1
        end
        achlock = achlock + 1
    end)
    lb_sjsk = CCLabelTTF:create(string.format("成就                     %d / %d",achUnlock,achlock ), "Arial", FontSize4)
    lb_sjsk:setColor(ccc3(115, 229, 253))--颜色
    lb_sjsk:setAnchorPoint(ccp(0.5, 0.5))
    lb_sjsk:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight - 100))
    layerMain:addChild(lb_sjsk,10)
    
    
    --- 加载生成成就列表
    -- 已经完成的成就优先排序
    
    local _achlist = {}
    local _bpx = DWinSizeWidth/2
    local _bpy = 400
    local _invH = 90
    local i = 0
    local sum = 0
    
    
    local _spw = 204
    local _sph = 230
    
    local pageFirst = 4
    local pageSums = 9
    
    local achsum = 0
    table.foreach(Achieve_Infos, function(k,v) achsum = achsum + 1 end)
    
    local _tableLayers = {}
    
    
    local container = GameContainerPage:new(DWinSizeWidth, 0)
    container:addToLayer(layer)
    
    
    local _LayerIndex = -1
    local _layer_items = nil
    local _batchNode = nil
    local _batchAchieveNode = nil
    
    while i < table.maxn(Achieve_Infos) do
        if Achieve_Infos[i] ~= nil then
            sum = sum + 1
            
            local indexVV = math.ceil((sum-pageFirst)/pageSums)
            if sum < pageFirst then
                indexVV = 0
            end
            
            if _LayerIndex ~= indexVV then
                _LayerIndex = indexVV
                
                _layer_items = CCLayer:create()
                _batchNode = CCSpriteBatchNode:create(s_MainUI2, achsum * 2)
                _layer_items:addChild(_batchNode)
                
                if indexVV == 0 then
                    layerMain:addChild(_layer_items)
                    _layer_items = layerMain
                end
                _layer_items:setPosition(gdccp(DWinSizeWidth * indexVV, 0))
                
                container:addChild(_layer_items)
            end
            
            
            local cach = AchieveClass:new(i)
            cach.spr_bg = CCSprite:createWithSpriteFrameName("acheveItem.png")
            _batchNode:addChild(cach.spr_bg)
            if Achieve_IconDatas[cach.iico] ~= nil then
                cach.spr_icon = CCSprite:createWithSpriteFrameName(Achieve_IconDatas[cach.iico])
                cach.spr_icon:setScale(0.44)
                _batchNode:addChild(cach.spr_icon)
            end
            
            local index = sum - 1
            if index >= pageFirst then
                _bpy = DWinSizeHeight - 100
                index = ((index-pageFirst)%pageSums)
            end
            
            cach:setPosition(_bpx, _bpy - index * _invH)
            cach:addToChild(_layer_items)
            
            --- 页码
            if indexVV ~= 0 then
                --local txtPage = GameNumbers:new(vNumberScore, NumberAlginment_Right)
                ----local txtPage = CCLabelTTF:create(indexVV, "Arial", FontSize)
                --txtPage:setPosition(gdccp(DWinSizeWidth/2,DWinSizeHeight-30))
                --txtPage:addLayer(_layer_items)
                --txtPage:setNumber(string.format("-%d-",indexVV))
                ----_layer_items:addChild(txtPage)
                
                local txtPage = CCLabelTTF:create(string.format("- %d -",indexVV), "Arial", FontSize4)
                txtPage:setPosition(gdccp(DWinSizeWidth/2,DWinSizeHeight-30))
                _layer_items:addChild(txtPage)
            end
            
        end
        i = i + 1
    end

    return layer
end

--------------------------------
-- Click And Move Test
--------------------------------
function Create_Achieve_Scene(stage)
    local scene = CCScene:create()
    layer = CCLayer:create()

    initWithLayer(stage)

    scene:addChild(layer)
    scene:addChild(CreateBackMenuItem())

    return scene, UI_Achieve
end
